package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.spu.SpuDTO;
import io.github.talelin.latticy.service.SpuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.SpuDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

/**
* @author generator@lms
* @since 2021-04-27
*/
@RestController
@RequestMapping("/v1/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;

    @PostMapping("")
    @PermissionMeta("创建商品")
    @GroupRequired
    @Logger(template = "{user.nickname}创建了一篇商品")
    public CreatedVO create(@RequestBody SpuDTO spuDTO ) {
        SpuDO spuDO = new SpuDO();
        BeanUtils.copyProperties(spuDTO, spuDO);
        spuService.save(spuDO);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestBody SpuDTO spuDTO) {

        SpuDO s = new SpuDO();
        s.setId(id);
        BeanUtils.copyProperties(spuDTO, s);
        spuService.updateById(s);
        return new UpdatedVO();
    }
    @PermissionMeta("删除商品")
    @GroupRequired
    @Logger(template = "{user.nickname}删除了一ge商品")
    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        spuService.removeById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public SpuDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        SpuDO byId = spuService.getById(id);
        return byId;
    }

    @GetMapping("/page")
    public PageResponseVO<SpuDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<SpuDO> pager = new Page<>(page, count);
        IPage<SpuDO> paging = spuService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }

    @PutMapping("/check/{id}")
    @PermissionMeta("审核商品")
    @GroupRequired
    @Logger(template = "{user.nickname}审核了一件商品")
    public UpdatedVO check(
            @RequestParam Integer online,
            @PathVariable @Positive(message = "{id.positive}") Integer id) {
        spuService.checkSpu(id,online);
        return new UpdatedVO();
    }

}
